<?php

/**
 * @author Immanuel Bauer | 0626117
 */
class Workinterval {
	
	/** 
	 * @var int
	 */
	private $id;
	
	/**
	 *  @var int
	 */
	private $userID;
	
	/**
	 *  @var int
	 */
	private $todoID;
	
	/**
	 *  @var timestamp
	 */
	private $startDate;
	
	/**
	 *  @var timestamp
	 */
	private $endDate;
	
	/**
	 * Creates a new object of the model and fills it with the given data.
	 * @param int $userID
	 * @param int $todoID
	 * @param timestamp $startDate
	 * @param timestamp $endDate
	 */
	function __construct($userID, $todoID, $startDate, $endDate) {
		$this->id = null;
		$this->userID = (int)$userID;
		$this->todoID = (int)$todoID;
		$this->startDate = (int)$startDate;
		$this->endDate = (int)$endDate;
	}
	
	/**
	 * Fetches the workinterval data for the given id and returns a Workinterval object with the data.
	 * Returns null if the workinterval doesn't exist.
	 * 
	 * @param int $id
	 * @return Workinterval
	 */
	public static function getWorkinterval($id){
		$qry = mysql_query('SELECT * FROM '.TABLE_PREFIX.'workintervals WHERE id = \''.mysql_real_escape_string($id).'\';');
		$row = mysql_fetch_assoc($qry);
		if(isset($row['id'])){
			$wi = new Workinterval($row['user'], $row['todo'], $row['start_date'], $row['end_date']);	
			$wi->setID($row['id']);
			return $wi;
		} else {
			return null;
		}
	}
	
	/**
	 * Fetches the Workintervals for the given parameters
	 * 
	 * @param int $todoID
	 * @param int $userID
	 * @return array<Workinterval>
	 */
	public static function getWorkintervals($todoID, $userID){
		$a = array();
		
		$where = '';
		
		if(!is_null($todoID)) $where = 'todo = \''.mysql_real_escape_string($todoID).'\'';
		if(!is_null($userID)) {
			if($where != '') $where .= ' AND ';
			$where = 'user = \''.mysql_real_escape_string($userID).'\'';
		}
		
		$qry = mysql_query('SELECT * FROM '.TABLE_PREFIX.'workintervals WHERE '.$where.';');
		while($row = mysql_fetch_assoc($qry)){
			$wi = new Workinterval($row['user'], $row['todo'], $row['start_date'], $row['end_date']);	
			$wi->setID($row['id']);
			array_push($a, $wi);
		} 
		return $a;
	}
	
	/**
	 * Saves the current state of the model object into the database
	 * @return boolean true if save was successfull
	 */
	public function saveToDB(){
		if(is_null($this->id)){
			//insert
			$qry = mysql_query('INSERT INTO '.TABLE_PREFIX.'workintervals (`id`, `user`, `todo`, `start_date`, `end_date`) VALUES (
				\'\',
				\''.mysql_real_escape_string($this->userID).'\',
				\''.mysql_real_escape_string($this->todoID).'\',
				\''.mysql_real_escape_string($this->startDate).'\',
				\''.mysql_real_escape_string($this->endDate).'\'
			);');
			if($qry){
				$qry = mysql_query('SELECT id FROM '.TABLE_PREFIX.'workintervals
					 WHERE `user` = \''.mysql_real_escape_string($this->userID).'\'
					 	AND `todo` = \''.mysql_real_escape_string($this->todoID).'\' 
					 	AND `start_date` = \''.mysql_real_escape_string($this->startDate).'\'
					 	AND `end_date` = \''.mysql_real_escape_string($this->endDate).'\'
				;');
				$row = mysql_fetch_assoc($qry);
				if(isset($row['id'])){
					$this->id = (int)$row['id'];
					return true;
				} else{
					return false;
				}
			} else {
				return false;
			}
		} else {
			//update
			$qry = mysql_query('UPDATE '.TABLE_PREFIX.'workintervals SET 
				`user` = \''.mysql_real_escape_string($this->userID).'\',
				`todo` = \''.mysql_real_escape_string($this->todoID).'\',
				`start_date` = \''.mysql_real_escape_string($this->startDate).'\',
				`end_date` = \''.mysql_real_escape_string($this->endDate).'\'
			WHERE id = \''.mysql_real_escape_string($this->id).'\';');
			return $qry;
		}
	}
	
	/**
	 * Removes the model object's corresponding row in the database and returns true if successfull.
	 * @return boolean
	 */
	public function removeFromDB(){
		if(!is_null($this->id)){
			return mysql_query('DELETE FROM '.TABLE_PREFIX.'workintervals WHERE id = \''.mysql_real_escape_string($this->id).'\';');
		} else {
			return true;
		}
	}
	
/* ####### GETTER ####### */
	
	/**
	 * The Workinterval's id
	 * @return int
	 */
	public function getID(){
		return (int)$this->id;
	}
	
	/**
	 * The id of the User who submitted this Workinterval
	 * @return int
	 */
	public function getUserID(){
		return (int)$this->userID;
	}
	
	/**
	 * The id of the Todo this Workinterval is linked to
	 * @return int
	 */
	public function getTodoID(){
		return (int)$this->todoID;
	}
	
	/**
	 * The Workinterval's start date.
	 * @return timestamp
	 */
	public function getStartDate(){
		return (int)$this->startDate;
	}
	
	/**
	 * The Workinterval's end date.
	 * @return timestamp
	 */
	public function getEndDate(){
		return (int)$this->endDate;
	}
	
/* ####### SETTER ####### */
	
	/**
	 * Sets the Workinterval's id
	 * @param int $id
	 */
	private function setID($id){
		$this->id = (int)$id;
	}
	
	/**
	 * Sets the id of the User who submitted this Workinterval
	 * @param int $id
	 */
	private function setUserID($id){
		$this->userID = (int)$id;
	}
	
	/**
	 * Sets the id of the Todo this Workinterval is linked to
	 * @param int $id
	 */
	public function setTodoID($id){
		$this->todoID = (int)$id;
	}
	
	/**
	 * Sets the Workinterval's start date.
	 * @param timestamp $date
	 */
	public function setStartDate($date){
		$this->startDate = (int)$date;
	}
	
	/**
	 * Sets the Workinterval's end date.
	 * @param timestamp $date
	 */
	public function getEndDate($date){
		$this->endDate = (int)$date;
	}
	
}

?>